import { defineStore } from 'pinia'
import * as deptApi from '../../../api/system/dept'
import type { Department } from '../../../types/app.d'

// 部门管理 Store 接口
interface DepartmentManagementState {
  deptList: Department[]
  deptTree: Department[]
  loading: boolean
  currentDept: Department | null
}

export const useDeptStore = defineStore('departmentManagement', {
  state: (): DepartmentManagementState => ({
    deptList: [],
    deptTree: [],
    loading: false,
    currentDept: null
  }),

  getters: {
    // 获取部门列表
    getDeptList(): Department[] {
      return this.deptList
    },

    // 获取部门树
    getDeptTree(): Department[] {
      return this.deptTree
    },

    // 获取加载状态
    getLoading(): boolean {
      return this.loading
    },

    // 获取当前部门
    getCurrentDept(): Department | null {
      return this.currentDept
    }
  },

  actions: {
    // 获取部门列表
    async getDeptListAction(params?: {
      status?: number
      name?: string
    }): Promise<void> {
      this.loading = true
      try {
        const response = await deptApi.getDeptList(params)
        if (response.code === 200) {
          this.deptList = response.data || []
        }
      } catch (error) {
        console.error('获取部门列表失败:', error)
      } finally {
        this.loading = false
      }
    },

    // 获取部门树
    async getDeptTreeAction(params?: {
      status?: number
    }): Promise<void> {
      try {
        const response = await deptApi.getDeptTree(params)
        if (response.code === 200) {
          this.deptTree = response.data || []
        }
      } catch (error) {
        console.error('获取部门树失败:', error)
      }
    },

    // 获取部门详情
    async getDeptInfoAction(id: string): Promise<void> {
      try {
        const response = await deptApi.getDeptInfo(id)
        if (response.code === 200) {
          this.currentDept = response.data
        }
      } catch (error) {
        console.error('获取部门详情失败:', error)
      }
    },

    // 创建部门
    async createDeptAction(data: Omit<Department, 'id' | 'createTime' | 'updateTime' | 'children'>): Promise<boolean> {
      try {
        const response = await deptApi.createDept(data)
        if (response.code === 200) {
          return true
        }
        return false
      } catch (error) {
        console.error('创建部门失败:', error)
        return false
      }
    },

    // 更新部门
    async updateDeptAction(id: string, data: Partial<Omit<Department, 'id' | 'createTime' | 'children'>>): Promise<boolean> {
      try {
        const response = await deptApi.updateDept(id, data)
        if (response.code === 200) {
          return true
        }
        return false
      } catch (error) {
        console.error('更新部门失败:', error)
        return false
      }
    },

    // 删除部门
    async deleteDeptAction(id: string): Promise<boolean> {
      try {
        const response = await deptApi.deleteDept(id)
        if (response.code === 200) {
          return true
        }
        return false
      } catch (error) {
        console.error('删除部门失败:', error)
        return false
      }
    },

    // 清空当前部门
    clearCurrentDept(): void {
      this.currentDept = null
    }
  }
})